package com.hex.liyue.giftmodule.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.hex.liyue.LiYueApplication;
import com.hex.liyue.R;
import com.hex.liyue.commonmodule.utils.DateUtils;
import com.hex.liyue.giftmodule.bean.HarvesGiftsBean;

import java.util.List;

/**
 * Created by WJ on 2016/6/12.
 */
public class AdapterHarvestGifts extends RecyclerView.Adapter<AdapterHarvestGifts.MyHolder> {
    private OnItemClickListener onItemClickListener;
    private List<HarvesGiftsBean.DataBean> list;

    public void setList(List<HarvesGiftsBean.DataBean> list) {
        this.list = list;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClickListener(int position);
    }

    @Override
    public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(parent.getContext(), R.layout.item_harvest_gifts, null);
        return new MyHolder(view);
    }

    @Override
    public void onBindViewHolder(MyHolder holder, final int position) {
        HarvesGiftsBean.DataBean dataBean = list.get(position);

        holder.rl_item_box.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemClickListener.onItemClickListener(position);
            }
        });
        holder.tv_harves_date.setText("订单时间：" + DateUtils.getFormatDate(dataBean.delivery_time * 1000, "yyyy-MM-dd"));
        holder.tv_harves_num.setText("收货单号：" + dataBean.user_gift_export_id);
        holder.tv_harves_prices.setText("商品金额：￥" + dataBean.total_cash);
        if (dataBean.status == 1) {
            holder.tv_harves_states.setText("收货状态：待收货");
        } else if (dataBean.status == 2) {
            holder.tv_harves_states.setText("收货状态：已收货");
        }
        Glide.with(LiYueApplication.getInstance())
                .load(dataBean.gift_list.get(0).gift_url).centerCrop().error(R.drawable.no_icon)
                .into(holder.iv_icon);
    }

    @Override
    public int getItemCount() {
        if (list == null) {
            return 0;
        } else {
            return list.size();
        }
    }

    class MyHolder extends RecyclerView.ViewHolder {
        RelativeLayout rl_item_box;
        TextView tv_harves_date;
        TextView tv_harves_num;
        TextView tv_harves_states;
        TextView tv_harves_prices;
        ImageView iv_icon;

        public MyHolder(View itemView) {
            super(itemView);
            rl_item_box = (RelativeLayout) itemView.findViewById(R.id.rl_item_box);
            tv_harves_date = (TextView) itemView.findViewById(R.id.tv_harves_date);
            tv_harves_prices = (TextView) itemView.findViewById(R.id.tv_harves_prices);
            tv_harves_num = (TextView) itemView.findViewById(R.id.tv_harves_num);
            tv_harves_states = (TextView) itemView.findViewById(R.id.tv_harves_states);
            iv_icon = (ImageView) itemView.findViewById(R.id.iv_icon);
        }
    }
}
